﻿using Parylene_CVD.Commands;
using Parylene_CVD.DAL;
using Parylene_CVD.Model;
using Parylene_CVD.Model.DModels;
using Parylene_CVD.Service;
using Parylene_CVD.Tools;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Parylene_CVD.ViewModel
{
    public class AlarmEventViewModel : ViewModelBase
    {
        public AlarmEventViewModel(NavigationService DeviceMonitorViewNavigationService, NavigationService DataCurveViewNavigationService, NavigationService DataRecordViewNavigationService, NavigationService HomeViewNavigationService) {
            DeviceName = GlobalVariables.CurDeviceName;
            SetDataSource();
            MonitorCommand = new NavigateCommand(DeviceMonitorViewNavigationService);
            DataRecordCommand = new NavigateCommand(DataRecordViewNavigationService);
            DataCurveCommand = new NavigateCommand(DataCurveViewNavigationService);
            ExitCommand = new NavigateCommand(HomeViewNavigationService);
            ComboBoxCommand = new DelegateCommand<Object>(ComboBoxSelectionChanged);
            NextPageCmd = new DelegateCommand(NextPage);
            PreviousPageCmd = new DelegateCommand(PreviousPage);
            LastPageCmd = new DelegateCommand(LastPage);
            FirstPageCmd = new DelegateCommand(FirstPage);
            CreateAlarmEvents();
            
    }


        public ICommand? MonitorCommand { get; }
        public ICommand? DataRecordCommand { get; }
        public ICommand? DataCurveCommand { get; }
        public ICommand? AlarmEventCommand { get; }
        public ICommand? ExitCommand { get; }
        // 设备名
        private string? _deviceName;
        public string? DeviceName
        {
            get => _deviceName;
            set
            {
                _deviceName = value;
                OnPropertyChanged(nameof(DeviceName));
            }
        }

        


        public List<AlarmEvents> dataSource { get; set; }

        public void SetDataSource()
        {
            DeviceDAL deviceDAL = new DeviceDAL();
            Device device = deviceDAL.GetDevice("DeviceName = '" + DeviceName + "'", "")[0];
            AlarmEventsDAL alarmEventsDAL = new AlarmEventsDAL();
            dataSource = alarmEventsDAL.GetAlarmEvents("DeviceID = " + device.DeviceId,"");
        }

        /// <summary>
        /// 页容量初始化
        /// </summary>
        public int pagesize = 10;
        /// <summary>
        /// 最后一页事件
        /// </summary>
        public DelegateCommand LastPageCmd { get; private set; }
        /// <summary>
        /// 第一页事件
        /// </summary>
        public DelegateCommand FirstPageCmd { get; private set; }
        /// <summary>
        /// 上一页事件
        /// </summary>
        public DelegateCommand PreviousPageCmd { get; private set; }
        /// <summary>
        /// 下一页事件
        /// </summary>
        public DelegateCommand NextPageCmd { get; private set; }


        /// <summary>
        /// 下拉事件
        /// </summary>
        public DelegateCommand<Object> ComboBoxCommand { get; private set; }
        /// <summary>
        /// 第几页
        /// </summary>
        private int pageNumber;

        public int PageNumber
        {
            get { return pageNumber; }
            set
            {
                pageNumber = value;
                OnPropertyChanged(nameof(PageNumber));
            }
        }
        /// <summary>
        /// 记录条数
        /// </summary>
        private int totalRecord;

        public int TotalRecord
        {
            get { return totalRecord; }
            set
            {
                totalRecord = value;
                OnPropertyChanged(nameof(TotalRecord));
            }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        private int totalPage;

        public int TotalPage
        {
            get { return totalPage; }
            set
            {
                totalPage = value;
                OnPropertyChanged(nameof(TotalPage));
            }
        }

        /// <summary>
        /// 是否可以执行上一页
        /// </summary>
        private bool canPrevious;

        public bool CanPrevious
        {
            get { return canPrevious; }
            set
            {
                canPrevious = value;
                OnPropertyChanged(nameof(CanPrevious));
            }
        }
        /// <summary>
        /// 是否可以执行上一页
        /// </summary>
        private bool canNext;

        public bool CanNext
        {
            get { return canNext; }
            set
            {
                canNext = value;
                OnPropertyChanged(nameof(CanNext));
            }
        }


        /// <summary>
        /// 分页后的数据
        /// </summary>
        private ObservableCollection<AlarmEvents> alarmEvents;
        public ObservableCollection<AlarmEvents> AlarmEvents
        {
            get { return alarmEvents; }
            set { alarmEvents = value; OnPropertyChanged(nameof(AlarmEvents)); }
        }




        private void FirstPage()
        {
            PageNumber = 1; //增加一页
            CanPrevious = false;
            if (pageNumber > 1)
            {
                CanPrevious = true; //是否可以点击上一页 否
            }
            TotalRecord = dataSource.Count; //总记录数
            int pages = TotalRecord / pagesize;
            TotalPage = TotalRecord % pagesize == 0 ? pages : pages + 1; //总页数
            if (TotalPage > PageNumber)
            {
                CanNext = true;//是否可以点击下一页
            }
            else
            {
                CanNext = false;
            }
            AlarmEvents = new ObservableCollection<AlarmEvents>();
            var data = dataSource.Skip((PageNumber - 1) * pagesize).Take(pagesize);
            foreach (var item in data)
            {
                AlarmEvents.Add(item);
            }
        }

        /// <summary>
        /// 最后一页
        /// </summary>
        private void LastPage()
        {
            CanPrevious = false;
            TotalRecord = dataSource.Count; //总记录数
            int pages = TotalRecord / pagesize;
            TotalPage = TotalRecord % pagesize == 0 ? pages : pages + 1; //总页数
            PageNumber = TotalPage;//最后一页 将总页码赋值给当前页
            if (pageNumber > 1)
            {
                CanPrevious = true; //是否可以点击上一页 否
            }
            if (TotalPage > PageNumber)
            {
                CanNext = true;//是否可以点击下一页
            }
            else
            {
                CanNext = false;
            }
            AlarmEvents = new ObservableCollection<AlarmEvents>();
            var data = dataSource.Skip((PageNumber - 1) * pagesize).Take(pagesize);
            foreach (var item in data)
            {
                AlarmEvents.Add(item);
            }
        }

        /// <summary>
        /// 下一页
        /// </summary>
        private void NextPage()
        {
            PageNumber++; //增加一页
            CanPrevious = false;
            if (pageNumber > 1)
            {
                CanPrevious = true; //是否可以点击上一页 否
            }
            TotalRecord = dataSource.Count; //总记录数
            int pages = TotalRecord / pagesize;
            TotalPage = TotalRecord % pagesize == 0 ? pages : pages + 1; //总页数
            if (TotalPage > PageNumber)
            {
                CanNext = true;//是否可以点击下一页
            }
            else
            {
                CanNext = false;
            }
            AlarmEvents = new ObservableCollection<AlarmEvents>();
            var data = dataSource.Skip((PageNumber - 1) * pagesize).Take(pagesize);
            foreach (var item in data)
            {
                AlarmEvents.Add(item);
            }
        }

        private void PreviousPage()
        {
            PageNumber--; //增加一页
            CanPrevious = false;
            if (pageNumber > 1)
            {
                CanPrevious = true; //是否可以点击上一页 否
            }
            TotalRecord = dataSource.Count; //总记录数
            int pages = TotalRecord / pagesize;
            TotalPage = TotalRecord % pagesize == 0 ? pages : pages + 1; //总页数
            if (TotalPage > PageNumber)
            {
                CanNext = true;//是否可以点击下一页
            }
            else
            {
                CanNext = false;
            }
            AlarmEvents = new ObservableCollection<AlarmEvents>();
            var data = dataSource.Skip((PageNumber - 1) * pagesize).Take(pagesize);
            foreach (var item in data)
            {
                AlarmEvents.Add(item);
            }
        }

        /// <summary>
        /// 选择每页显示记录数
        /// </summary>
        /// <param name="obj"></param>
        private void ComboBoxSelectionChanged(Object obj)
        {
            if (obj != null)
            {
                var Content = ((System.Windows.Controls.ContentControl)obj).Content;
                switch (Content)
                {
                    case "10条/页": pagesize = 10; ComboBoxSelectionInitData(); break;
                    case "20条/页": pagesize = 20; ComboBoxSelectionInitData(); break;
                    case "30条/页": pagesize = 30; ComboBoxSelectionInitData(); break;
                }
            }
        }

        /// <summary>
        /// 每页显示记录数改变时 重新加载数据
        /// </summary>
        void ComboBoxSelectionInitData()
        {
            //
            PageNumber = 1; //页码改变时 加载第一页
            CanPrevious = false; //是否可以点击上一页 否
            TotalRecord = dataSource.Count; //总记录数
            int pages = TotalRecord / pagesize;
            TotalPage = TotalRecord % pagesize == 0 ? pages : pages + 1; //总页数
            if (TotalPage > 1)
            {
                CanNext = true;//是否可以点击下一页
            }
            AlarmEvents = new ObservableCollection<AlarmEvents>();
            var data = dataSource.Skip((PageNumber - 1) * pagesize).Take(pagesize);
            foreach (var item in data)
            {
                AlarmEvents.Add(item);
            }
        }

        public void CreateAlarmEvents()
        {
            PageNumber = 1; //默认加载第一页
            CanPrevious = false; //是否可以点击上一页 否
            TotalRecord = dataSource.Count; //总记录数
            int pages = TotalRecord / pagesize;
            TotalPage = TotalRecord % pagesize == 0 ? pages : pages + 1; //总页数
            if (TotalPage > 1)
            {
                CanNext = true;//是否可以点击下一页
            }
            AlarmEvents = new ObservableCollection<AlarmEvents>();
            var data = dataSource.Skip((PageNumber - 1) * pagesize).Take(pagesize);
            foreach (var item in data)
            {
                AlarmEvents.Add(item);
            }
        }
    }
}
